package com.hipmarket.platform.security

import com.hipmarket.platform.security.storage.JwtAuthenticationStorage
import org.springframework.security.authentication.AuthenticationProvider
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder

class JwtAuthenticationProvider(private val storage: JwtAuthenticationStorage): AuthenticationProvider {

    override fun authenticate(p0: Authentication): Authentication {
        val authentication = p0 as JwtAuthenticationToken
        val token = authentication.token
        val user = storage.findUserByToken(token)
        SecurityContextHolder.getContext().authentication = user
        return user
    }

    override fun supports(authentication: Class<*>): Boolean {
        return JwtAuthenticationToken::class.java.isAssignableFrom(authentication)
    }

}